﻿using System;
using System.Windows.Forms;
using WebBase;

namespace MainDisk
{
    public partial class Form1 : Form
    {
        private ScriptEngine scriptEngine;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            scriptEngine.Reset();
            scriptEngine.Language =
                (ScriptLanguage) Enum.Parse(typeof (ScriptLanguage), comboBoxScript.SelectedItem.ToString());
            scriptEngine.Timeout = (int) numericUpDownTimeout.Value;
            scriptEngine.AllowUI = checkBoxAllowUI.Checked;
            if (radioButtonEval.Checked) //执行Eval方法
            {
                textBoxResult.Text =
                    scriptEngine.Eval(textBoxMethodName.Text + "(" + textBoxParams.Text + ")", textBoxCodeBody.Text).
                        ToString();
            }
            else //执行Run方法
            {
                string[] parameters = textBoxParams.Text.Split(',');
                var paramArray = new object[parameters.Length];
                for (int i = 0; i < parameters.Length; i++)
                    paramArray[i] = Int32.Parse(parameters[i]);

                textBoxResult.Text =
                    scriptEngine.Run(textBoxMethodName.Text, paramArray, textBoxCodeBody.Text).ToString();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBoxScript.SelectedIndex = 2;
            textBoxCodeBody.Text = "function timeOutTest(x){return 100;}";
            scriptEngine = new ScriptEngine();
//            scriptEngine.UseSafeSubset = true;
            scriptEngine.RunError += scriptEngine_RunError;
            scriptEngine.RunTimeout += scriptEngine_RunTimeout;
        }

        //错误函数


        private void scriptEngine_RunError()
        {
            MessageBox.Show("RunError执行脚本错误!");
        }


        private void scriptEngine_RunTimeout()
        {
            MessageBox.Show("RunTimeout执行脚本超时，引发错误!");
        }
    }
}